package com.lw.carmanage.events.listeners;

import com.lw.carmanage.events.MyUserRoleEvent;
import com.lw.carmanage.mapper.SysUserMapper;
import com.lw.carmanage.mapper.SysUserRoleMapper;
import com.lw.carmanage.pojo.SysUser;
import com.lw.carmanage.pojo.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/3/31 15:12
 */
@Component
public class MyUserRoleEventListener {


    private SysUserMapper userMapper;

    @Autowired
    public void setUserMapper(SysUserMapper userMapper) {
        this.userMapper = userMapper;
    }

    private SysUserRoleMapper userRoleMapper;

    @Autowired
    public void setUserRoleMapper(SysUserRoleMapper userRoleMapper) {
        this.userRoleMapper = userRoleMapper;
    }

    @EventListener
    public void onEvent(MyUserRoleEvent event){
            System.err.println("收到注册成功的事件了");
            //添加注册用户的角色权限
        SysUser sysUser = userMapper.findByUserName(event.getUsername());
        SysUserRole userRole=new SysUserRole();

        userRole.setUserId(sysUser.getUserId());
        userRole.setRoleId(6L);
        userRoleMapper.addUserRole(userRole);
    }
}
